<?php


namespace app\admin\controller;

use app\common\lib\Status;

/**
 * 规格
 * Class Specs
 * @package app\admin\controller
 */
class SpecsValue extends AdminBase
{

    //获取规格属性下的数据
    public function getBySpecsId()
    {
        //获取规格属性id
        $specsId = input("param.specs_id", 0, "intval");

        if (!$specsId) {
            return show(config('status.success'), "没有数据");
        }

        //获取规格属性逻辑
        $result = (new \app\common\business\SpecsValue());
        $result = $result->getBySpecsId($specsId);

        return show(config('status.success'), "ok", $result);
    }


    //新增逻辑
    public function save()
    {
        //接收规格id跟属性内容
        $specsId = input("param.specs_id", 0, "intval");
        $name = input("param.name", "", "trim");

        $data = [
            "specs_id" => $specsId,
            "name" => $name,
        ];

        //validate验证数据
        $validate = new \app\admin\validate\SpecsValue();
        if (!$validate->scene('save')->check($data)) {
            return show(config("status.error"), $validate->getError());
        }
        //halt($data);
        $id = (new \app\common\business\SpecsValue())->add($data);
        if (!$id) {
            return show(config('status.error'), "新增失败");
        }
        return show(config('status.success'), "ok", ["id" => $id]);

    }

    //删除更改状态
    public function delete()
    {
        //接收数据
        $status = input("param.status", 0, "intval");
        $id = input("param.id", 0, "intval");

        //验证
        $data = [
            'status' => $status,
            'id' => $id,
        ];
        $validate = new \app\admin\validate\SpecsValue();
        if (!$validate->scene('delete')->check($data)) {
            return show(config('status.error'), $validate->getError());
        }
        if (!$id || !in_array($status, Status::getTableStatus())) {
            return show(config('status.error'), '参数错误');
        }

        //删除逻辑 更改状态为99
        try {
            $res = (new \app\common\business\SpecsValue())->status($id, $status);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }
        if ($res) {
            return show(config('status.success'), '更新状态成功');
        } else {
            return show(config('status.error', '更新状态是不'));
        }

    }


}